package com.example.goandpaytthebill.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.goandpaytthebill.Bean.loginBean;
import com.example.goandpaytthebill.R;
import com.example.goandpaytthebill.ServerApi;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Call;

public class LoginActivity extends AppCompatActivity {

    private ImageView iv_zhanshi;
    private EditText et_password;
    private boolean canSee=true;
    private Button bt_denglu;
    private EditText et_phone;
    private ArrayList<loginBean> loginBeanList = new ArrayList<>();
    private TextView tv_yzm;
    private ImageView iv_back;
    private TextView tv_zhuce;
    private TextView tv_wangji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
            LoginActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setView();
        initView();
//        Intent intent=new Intent(LoginActivity.this,RegisterActicity.class);
//        startActivity(intent);
    }

    private void initView() {
        tv_wangji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(LoginActivity.this, ChangePasswordActivity.class);
                startActivity(intent);
            }
        });
        tv_zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActicity.class);
                intent.putExtra("inttype","0");
                startActivity(intent);
                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_yzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,VerificationCodeLogin.class);
                startActivity(intent);
                finish();
            }
        });
        bt_denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_password.getText().toString().equals("")){
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    if (et_phone.getText().toString().equals("")){
                        Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    }else {
                        login();
                    }
                }
            }
        });
        iv_zhanshi.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (canSee == false) {
                    //如果是不能看到密码的情况下，
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    iv_zhanshi.setBackgroundResource(R.mipmap.login_yicang);
                    canSee = true;
                } else {
                    //如果是能看到密码的状态下
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    iv_zhanshi.setBackgroundResource(R.mipmap.login_zhanshi);
                    canSee = false;

                }

            }
        });
    }


    private void login() {
        OkHttpUtils
                .post()
                .url(ServerApi.login)
                .addParams("mobile",et_phone.getText().toString())
                .addParams("code",et_phone.getText().toString())

                .build()
                .execute(new StringCallback() {
                    private String msg;
                    private String code;

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            code = new JSONObject(response).getString("code");
                            msg = new JSONObject(response).getString("msg");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
                        if (code.equals("1")){
                            ArrayList ListUsers = new ArrayList<String>();
                            ListUsers.add(response);
                            Gson gson = new Gson();
                            ArrayList<loginBean> outlines = gson.fromJson(String.valueOf(ListUsers), new TypeToken<ArrayList<loginBean>>() {
                            }.getType());
                            for (int i = 0; i < outlines.size(); i++) {
                                loginBean wxLoginBean = outlines.get(i);
                                loginBeanList.add(new loginBean(
                                        wxLoginBean.getCode(),
                                        wxLoginBean.getMsg(),
                                        wxLoginBean.getData()
                                ));

                            }
                            SharedPreferences.Editor editor= getSharedPreferences("logInfo", MODE_PRIVATE).edit();
                            editor.putString("username",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getUsername()));
                            editor.putString("nickname",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getNickname()));
                            editor.putString("mobile",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getMobile()));
                            editor.putString("avatar",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getAvatar()));
                            editor.putString("score",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getScore()));
                            editor.putString("token",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getToken()));
                            editor.putString("user_id",String.valueOf(loginBeanList.get(0).getData().getUserinfo().getUser_id()));
                            editor.commit();
                            finish();
                        }

                    }
                });


    }

    private void setView() {
        iv_zhanshi=findViewById(R.id.iv_zhanshi);
        tv_wangji=findViewById(R.id.tv_wangji);
        tv_yzm=findViewById(R.id.tv_yzm);
        tv_zhuce=findViewById(R.id.tv_zhuce);
        et_password=findViewById(R.id.et_password);
        bt_denglu=findViewById(R.id.bt_denglu);
        et_phone=findViewById(R.id.et_phone);
        iv_back=findViewById(R.id.iv_back);
    }
}